<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Friend_info extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model('friendtop_model');
		session_start();
	}
	public function midToStr($mid) {   //将mid转化成短字符串的形式
		settype($mid, 'string');
		$mid_length = strlen($mid);
		$url = '';
		$str = strrev($mid);
		$str = str_split($str, 7);

		foreach ($str as $v) {
			$char = $this->intTo62(strrev($v));
			$char = str_pad($char, 4, "0");
			$url .= $char;
		}
		$url_str = strrev($url);
		return ltrim($url_str, '0');
	}

	public function str62keys_int_62($key) //62进制字典
	{
		$str62keys = array (
			"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q",
			"r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
			"R","S","T","U","V","W","X","Y","Z"
		);
		return $str62keys[$key];
	}

	/* url 10 进制 转62进制*/

	public function intTo62($int10) {
		$s62 = '';
		$r = 0;
		while ($int10 != 0) {
			$r = $int10 % 62;
			$s62 .= $this->str62keys_int_62($r);
			$int10 = floor($int10 / 62);
		}
		return $s62;
	}



	public function index()
	{
		//$temp=$this->session->userdata('access_token');
		$temp=$_SESSION['token']['access_token'];
		//var_dump($temp);
		$update_time=$this->session->userdata("update_time");
		if ( empty($temp))   //如果用户没登录
		{
			//echo "empty";
			$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
			$code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
			echo "<meta http-equiv=refresh content='0; url=$code_url'>";  //跳转到授权页面
		}
		else
		{
			//echo "OK";
			$c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$temp );

			$uid_get=$c->get_uid();   //获取当前登录用户的UID
			$uid=$uid_get['uid'];
			$fid=$this->input->post("id");	//从GET类型的AJAX请求中获取当前登录用户所指定的互粉用户的UID
			//var_dump($fid);

			$response=array();
			$weibo_info='';

			if( !isset($_SESSION['weibos']) || ((strtotime('now') - $update_time)/60)>30)	//如果weibos为空或者距离上次更新时间超过30分钟
			{
				$weibo=$c->user_timeline_by_id($fid,1,100);	//取出100条最新微博
			}
			else
			{
				$weibo=$_SESSION['weibos']["$fid"];
			}
			//var_dump($_SESSION['weibos'];
			//从上面API调回的微博中取出今天发布的和更早发布的，并将其分开赋值
			$count_today=0;	//对今天发布的微博计数
			$today=date("d");	//今天的日期（天）
			$creat_time=strtotime($weibo['statuses'][$count_today]['created_at']);	//最新一条微博的日期（天）
			$weibo_today=array();  //今天发布的微博（数组）
			$weibo_before=array();	//更早发布的微博（数组）
			
			while(date("d",$creat_time) == $today)
			{
				$weibo_today[$count_today]['weibo']=$weibo['statuses'][$count_today];	//将本条微博信息赋值给$weibo_today
				$weibo_today[$count_today]['mid']=$this->midToStr($weibo['statuses'][$count_today]['mid']);	//字符短串形式的微博mid
				$count_today++;	//指向下一条微博
				$creat_time=strtotime($weibo['statuses'][$count_today]['created_at']);	//最新一条微博的日期（天）
			}

			/*do
			{
				$weibo_today[$count_today]['weibo']=$weibo['statuses'][$count_today];	//将本条微博信息赋值给$weibo_today
				$weibo_today[$count_today]['mid']=$this->midToStr($weibo['statuses'][$count_today]['mid']);	//字符短串形式的微博mid
				$count_today++;	//指向下一条微博
				$creat_time=strtotime($weibo['statuses'][$count_today]['created_at']);	//本条微博发布的时间（天）
			}while(date("d",$creat_time) == $today);  //如果是今天发布的微博*/
			
			for($i=0; $i<10; $i++)
			{
				$weibo_before[$i]['weibo']=$weibo['statuses'][$count_today];
				$weibo_before[$i]['mid']=$this->midToStr($weibo['statuses'][$count_today]['mid']);
				$count_today++;
			}
			//var_dump($weibo_today);
			//var_dump($weibo_before);

			//解析今天发布的微博和更早发布的微博中的@信息和短链接信息
			//将读取的微博里的短链接转化成超链接
			$pattern_url="/(http:\/\/[a-zA-Z0-9.\/]+)/";  //匹配短链接的正则表达式
			$pattern_at="|(@[\x{4e00}-\x{9fa5}A-Za-z0-9_-]+)|u";        //匹配@某用户的正则表达式

			//解析今天的微博
			//if( ! empty($weibo_today))
			if(count($weibo_today,1)>0)	//如果今天微博有更新
			{
				foreach ($weibo_today as &$item)
				{
					//将每一条微博中的短链接转化为超链接形式
					if(preg_match_all($pattern_url, $item['weibo']['text'], $matches,PREG_PATTERN_ORDER))  //如果能找到短链接
					{
						foreach($matches[0] as $match)   //将该微博内容中的每一条短链接转化成超链接
						{
							$replace="<a style='text-decoration:none'  target='_blank' href=".$match.">".$match."</a>";    //完整的超链接
							$item['weibo']['text']=str_replace($match, $replace, $item['weibo']['text']);   //用超链接替换博文中的短链接
						}
					}

					//如果非原创微博，则把原微博中的短链接转化为超链接形式
					if(isset($item['weibo']['retweeted_status']))
					{
						if(preg_match_all($pattern_url, $item['weibo']['retweeted_status']['text'], $matches,PREG_PATTERN_ORDER))
						{
							foreach($matches[0] as $match)
							{
								$replace="<a style='text-decoration:none' target='_blank' href=".$match.">".$match."</a>";
								$item['weibo']['retweeted_status']['text']=str_ireplace($match, $replace, $item['weibo']['retweeted_status']['text']);
							}
						}
					}

					//将微博中的@用户信息转化为超链接形式
					if(preg_match_all($pattern_at, $item['weibo']['text'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
					{
						foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
						{
							$name=strtok($match, "@");  //将用户昵称识别出来
							//$user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
							$replace="<a class='at' name='$name'>@".$name."&nbsp</a>";    //完整的超链接
							$item['weibo']['text']=str_replace($match, $replace, $item['weibo']['text']);   //用超链接替换博文中的@信息
						}
					}

					//如果非原创微博，则把原微博中的@用户信息转化为超链接形式
					if(isset($item['weibo']['retweeted_status']))
					{
						if(preg_match_all($pattern_at, $item['weibo']['retweeted_status']['text'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
						{
							foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
							{
								$name=strtok($match, "@");  //将用户昵称识别出来
								//$user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
								$replace="<a class='at' name='$name'>@".$name."&nbsp</a>";    //完整的超链接
								$item['weibo']['retweeted_status']['text']=str_replace($match, $replace, $item['weibo']['retweeted_status']['text']);   //用超链接替换博文中的@信息
							}
						}
					}
				}
			}

			//解析更早发布的微博
			//if( ! empty($weibo_before))
			if(count($weibo_before,1)>0)
			{
				foreach ($weibo_before as &$item)
				{
					//将每一条微博中的短链接转化为超链接形式
					if(preg_match_all($pattern_url, $item['weibo']['text'], $matches,PREG_PATTERN_ORDER))  //如果能找到短链接
					{
						foreach($matches[0] as $match)   //将该微博内容中的每一条短链接转化成超链接
						{
							$replace="<a style='text-decoration:none'  target=:'_blank' href=".$match.">".$match."</a>";    //完整的超链接
							$item['weibo']['text']=str_replace($match, $replace, $item['weibo']['text']);   //用超链接替换博文中的短链接
						}
					}

					//如果非原创微博，则把原微博中的短链接转化为超链接形式
					if(isset($item['weibo']['retweeted_status']))
					{
						if(preg_match_all($pattern_url, $item['weibo']['retweeted_status']['text'], $matches,PREG_PATTERN_ORDER))
						{
							foreach($matches[0] as $match)
							{
								$replace="<a style='text-decoration:none' target:'_blank' href=".$match.">".$match."</a>";
								$item['weibo']['retweeted_status']['text']=str_ireplace($match, $replace, $item['weibo']['retweeted_status']['text']);
							}
						}
					}

					//将微博中的@用户信息转化为超链接形式
					if(preg_match_all($pattern_at, $item['weibo']['text'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
					{
						foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
						{
							$name=strtok($match, "@");  //将用户昵称识别出来
							//$user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
							$replace="<a class='at' name='$name'>@".$name."&nbsp</a>";    //完整的超链接
							$item['weibo']['text']=str_replace($match, $replace, $item['weibo']['text']);   //用超链接替换博文中的@信息
						}
					}

					//如果非原创微博，则把原微博中的@用户信息转化为超链接形式
					if(isset($item['weibo']['retweeted_status']))
					{
						if(preg_match_all($pattern_at, $item['weibo']['retweeted_status']['text'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
						{
							foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
							{
								$name=strtok($match, "@");  //将用户昵称识别出来
								//$user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
								$replace="<a class='at' name='$name'>@".$name."&nbsp</a>";    //完整的超链接
								$item['weibo']['retweeted_status']['text']=str_replace($match, $replace, $item['weibo']['retweeted_status']['text']);   //用超链接替换博文中的@信息
							}
						}
					}
				}
			}
			

			//封装JSON
			$weibo_info="
				<div class='contentbox'><!--右侧内容模型-->";
			//if( ! empty($weibo_today))	//	如果今天有更新的微博
			if(count($weibo_today,1)>0)
			{
				$weibo_info.="<div class='w_today'><!--今日更新-->
				<img id='todayimg' src='http://192.168.8.79/friendtop/public/images/todayimg.png' />
				<div class='friendmaincontent'><ul>";
				//今天更新
				foreach($weibo_today as $item)
				{
					$weibo_info.="<li>
						<div class='friendcontent'>".$item['weibo']['text'];
					if(isset($item['weibo']['bmiddle_pic']))
					{	
						$weibo_info.="<div class='friendsubcontentimg'><a rel='next' class='miniImg artZoom' name='".$item['weibo']['bmiddle_pic']."'><img src=".$item['weibo']['thumbnail_pic']." /></a></div>";
					}
					elseif(isset($item['weibo']['retweeted_status']))
					{
						$weibo_info.="<div class='friendsubcontent' >";
						if(isset($item['weibo']['retweeted_status']['user']))
						{
							$weibo_info.="<a id='astyle' href='http://weibo.com/u/".(string)$item['weibo']['retweeted_status']['user']['id']."' target='_blank'><span>".$item['weibo']['retweeted_status']['user']['screen_name']."</span></a>:";
						}
						$weibo_info.=$item['weibo']['retweeted_status']['text'];
						if(isset($item['weibo']['retweeted_status']['bmiddle_pic']))
						{
							$weibo_info.="<div class='friendsubcontentimg'><a rel='next' class='miniImg artZoom' name='".$item['weibo']['retweeted_status']['bmiddle_pic']."'><img src=".$item['weibo']['retweeted_status']['thumbnail_pic']." /></div>";
						}
						$weibo_info.="</div>";
					}
					//加上时间和评论转发信息
					$weibo_info.="<p><span><a target='_blank' href='http://www.weibo.com/".(string)$fid."/".$item['mid']."' >转发(".$item['weibo']['reposts_count'].")</a><b style='margin:0 5px; color: #E6E6E6;'>|</b><a target='_blank' href='http://www.weibo.com/".(string)$fid."/".$item['mid']."' >评论(".$item['weibo']['reposts_count'].")</a></span>".date("m",strtotime($item['weibo']['created_at']))."月".date("d",strtotime($item['weibo']['created_at']))." ".date("H",strtotime($item['weibo']['created_at'])).":".date("i",strtotime($item['weibo']['created_at']))."</P>";

					$weibo_info.="
						<div class='line'></div>
						</div>
						</li>";


				}
				$weibo_info.="</ul>
				</div>
				</div>";
			}
			else
			{
				$weibo_info.="<img class='noupdate' src='http://192.168.8.79/friendtop/public/images/noUpdate.jpg' />";
			}
			$weibo_info.="
				<div class='w_earlyer'><!--昨天更新-->
				<img id='earlyerimg' src='http://192.168.8.79/friendtop/public/images/erlayerimg.png'/>
				<div class='friendmaincontent'>
				<ul>";

			//foreach($weibo_before as $item)
			for($j=0;$j<10;$j++)
			{
				$weibo_info.="<li>
					<div class='friendcontent'>".$item['weibo']['text'];
				if(isset($weibo_before[$j]['weibo']['bmiddle_pic']))
				{	
					$weibo_info.="<div class='friendsubcontentimg'><a rel='next' class='miniImg artZoom' name='".$weibo_before[$j]['weibo']['bmiddle_pic']."'><img src=".$weibo_before[$j]['weibo']['thumbnail_pic']." /></a></div>";
				}
				elseif(isset($weibo_before[$j]['weibo']['retweeted_status']))
				{
					$weibo_info.="<div class='friendsubcontent' >";
					if(isset($weibo_before[$j]['weibo']['retweeted_status']['user']))
					{
						$weibo_info.="<a id='astyle' href='http://weibo.com/u/".(string)$weibo_before[$j]['weibo']['retweeted_status']['user']['id']."' target='_blank'><span>".$weibo_before[$j]['weibo']['retweeted_status']['user']['screen_name']."</span></a>:";
					}
					$weibo_info.=$weibo_before[$j]['weibo']['retweeted_status']['text'];
					if(isset($weibo_before[$j]['weibo']['retweeted_status']['bmiddle_pic']))
					{
						$weibo_info.="<div class='friendsubcontentimg'><a rel='next' class='miniImg artZoom' name='".$weibo_before[$j]['weibo']['retweeted_status']['bmiddle_pic']."'><img src=".$weibo_before[$j]['weibo']['retweeted_status']['thumbnail_pic']." /></a></div>";
					}
					$weibo_info.="</div>";
				}
				$weibo_info.="<p><span><a target='_blank' href='http://www.weibo.com/".(string)$fid."/".$weibo_before[$j]['mid']."' >转发(".$weibo_before[$j]['weibo']['reposts_count'].")</a><b style='margin:0 5px; color: #E6E6E6;'>|</b><a target='_blank' href='http://www.weibo.com/".(string)$fid."/".$weibo_before[$j]['mid']."' >评论(".$weibo_before[$j]['weibo']['reposts_count'].")</a></span>".date("m",strtotime($weibo_before[$j]['weibo']['created_at']))."月".date("d",strtotime($weibo_before[$j]['weibo']['created_at']))." ".date("H",strtotime($weibo_before[$j]['weibo']['created_at'])).":".date("i",strtotime($weibo_before[$j]['weibo']['created_at']))."</P>";
				$weibo_info.="
					<div class='line'></div>
					</div>
					</li>";
			}
			$weibo_info.="</ul>
				</div>
				</div>
				</div>";
			$code=1;
			//var_dump($weibo_before);


			$response=array
				(
					'code'=>$code,
					'result'=>$weibo_info
				);
			echo json_encode($response);
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
